<template>
  <transition name="fade">
    <div class="notifi" v-show="show" :style="setStyle">
      <div class="content" >
        {{options.content}}
      </div>
      <div class="delete" v-if="!options.autoClose">
        <i class="iconfont icon-cuowutishixiao" @click="close" ></i>
      </div>
    </div>
  </transition>
</template>

<script type="text/ecmascript-6">
  export default {
    props: {
      options: {
        type: Object,
        default: () => {
          return {};
        }
      },
      show: {
        type: Boolean,
        default: false
      }
    },
    data () {
      return {
        timers: []
      };
    },
    computed: {
      setStyle () {
        return {
          color: this.options.textColor || '#fff',
          background: this.options.backgroundColor || '#21e7b6'
        };
      }
    },
    methods: {
      close () {
        this.show = false;
        this.options = {};
      },
      countdown () {
        if (this.options.autoClose) {
          const t = setTimeout(() => {
            this.close();
          }, this.options.showTime || 3000);
          this.timers.push(t);
        }
      }
    },
    watch: {
      options () {
        this.timers.forEach((timer) => {
          window.clearTimeout(timer);
        });
        this.timers = [];
        this.countdown();
      }
    }
  };
</script>

<style lang="scss" scoped>
  .notifi{
    position: absolute;
    top:0 ;
    width: 100%;
    padding: 15px 20px 15px;
    color: #fff;
    background-color:#21e7b6;

    .content{
      line-height: 24px;
      font-size:16px;
    }
    .delete{
      position: absolute;
      right: 2px;
      top:4px;

      i{
        font-size: 20px;
      }
    }
  }
</style>
